﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Customer
{
    public class ContractController : PageInfoBase
    {
        String cid = "";
        [ActionFilter(IsLogin = true)]       
        public ActionResult Index(String id, String keyCode, String cid, String ckeyCode)
        {
            if (!Fun.IDVerify(cid, ckeyCode))
                return Content(WebMessage.NoGrant);
            this.cid = cid;
            return Load("Contract", "Customer/Contract", id, keyCode);
        }
        protected override void FieldAddInit(DataRow data)
        {
            data["cid"] = cid;
            data["Sex"] = "男";
            data["IsKids"] = 0;          
            base.FieldAddInit(data);
        }

        protected override SqlState SqlExecute(BaseData bd, String id, Dictionary<String, Object> data)
        {
            if (id.Equals("-1"))
            {
                DataRow drUser = BasePage.GetUser(Session);
                String userId = drUser["iuserid"].ToString();
                String leagueId = Fun.cString(drUser["LeagueId"].ToString());

                String date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");              
                data.Add("CreateId", userId);
                data.Add("CreateDate", date);
                data.Add("LeagueId", leagueId);               
            }

            return SqlState.None;
        }

        [ActionFilter(IsLogin = true)]
        public JsonResult Data(String id, String keyCode) {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Error(WebMessage.NoGrant));
            String sql = String.Format(@"select id,'' keyCode,linkName,Sex,LinkTel,case when IsKids=1 then '是' else '否' end IsKids,t2.dname linkRelation
from cu_Cus_contacts t1
left
join tb_sys_dictionary t2 on t2.typeid = 'D04' and linkRelation = t2.did
where cid = {0}", id);
            Dictionary<String, Object>[] data = BasePage.bd.ExecuteDictArray(sql);
            foreach (Dictionary<String, Object> dr in data) {
                dr["keyCode"] = Fun.IDCode(dr["id"].ToString());
            }
            return BasePage.Json(ResultData.OK(data));
        }     
    }
}